package com.xinwei.leetcode.数组;

// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/
public class _80_删除有序数组中的重复项II {
    public int removeDuplicates(int[] nums) {
        int times = 1; // 用于记数
        int result = nums.length; // 用于记录删去重复项后还剩多少个数
        int i = 1;
        while (i < result){
            if (nums[i] == nums[i - 1]) {
                times++;
            }
            if (nums[i] != nums[i - 1]) {
                times = 1;
            }
            if (times > 2) {
                int j = i;
                while (j < result) {
                    if (nums[j] != nums[i]) break;
                    j++;
                }
                System.out.println(result + " " + j + " " + i);
                // 现在j是第一个不同的数
                result = result - (j - i);
                int k = i;
                while (j < nums.length) {
                    nums[k++] = nums[j++];
                }
                times = 1;
            }
            System.out.println("i = "+ i + "   times = " + times);
            i++;
        }
        return result;
    }

    // 双指针，快慢指针
    public int removeDuplicates1(int[] nums) {
        int len = nums.length;
        if (len <= 2) {
            return len;
        }
        int slow = 2;
        int fast = 2;
        while (fast < nums.length) {
            if (nums[slow - 2] != nums[fast]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;
    }


}
